package com.bit.book.aspect;

import com.bit.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class TimeRecordAspect {
    /**
     * 记录方法执行时间
     * @return
     */
    @Around("execution (* com.bit.book.controller.*.*(..)))")
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {
        /**
         * 1. 记录开始时间
         * 2. 执行目标方法
         * 3. 记录结束时间
         * 4. 记录耗时
         */
        long start = System.currentTimeMillis();
        //调用目标方法
        Object proceed = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature() + " 耗时: {} ms", end-start);
        return proceed;
    }
}
